package cn.hairui.blog.dao;

import cn.hairui.blog.entity.Type;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类表 Mapper 接口
 * </p>
 *
 * @author laoganbu
 * @since 2020-04-06
 */
@Mapper
public interface TypeMapper extends BaseMapper<Type> {
    @Select("select * from type limit 0,#{count}")
    List<Type> listTop(int count);

    @Select("select count(1) from type")
    int count();

    @Select("select\n" +
            "  t1.id,\n" +
            "  t1.name,\n" +
            "  count(1) as count\n" +
            "from type t1\n" +
            "  inner join artical t2\n" +
            "    on t1.id = t2.type_id\n" +
            "where t1.show_flag = \n" +
            "#{showFlag}\n" +
            "group by t1.id\n" +
            "order by count(1) desc\n" +
            "limit 0,#{count} ")
    List<Map> selectByShowFlagLimit(String showFlag, int count);

    @Select("select id,name,case when show_flag='Y' then 'checked' else '' end as showflag,(select count(1) from artical where type.id=artical.type_id) as count from type order by id asc")
    List<Map> listByManageShow();

    @Select("select\n" +
            "  t1.id,\n" +
            "  t1.name,\n" +
            "  count(1) as count\n" +
            "from type t1\n" +
            "  inner join artical t2\n" +
            "    on t1.id = t2.type_id\n" +
            "where t1.show_flag = \n" +
            "#{showFlag}\n" +
            "group by t1.id\n" +
            "order by count(1) desc")
    List<Map> selectByShowFlag(String showFlag);
}
